<template>
  <body>
  <div class="main-box">
    <div :class="['container', 'container-register', { 'is-txl': isLogin }]">
      <el-form ref="registerForm" :model="registerForm" :rules="registerFormRules">
        <h2 class="title">创建账号</h2>
        <span class="text"></span>
        <input class="form__input" type="text" placeholder="手机号" v-model="registerForm.phoneNumber"/>
        <br>

        <input class="form__input" type="text" placeholder="验证码" v-model="registerForm.repass"/>
        <el-button class="send-code" :disabled="countdown > 0" @click="sendCode">
          {{ countdown > 0 ? countdown + 's' : '发送验证码' }}
        </el-button>
        <div class="primary-btn" @click="submitForm">立即注册</div>
      </el-form>
    </div>
    <div
        :class="['container', 'container-login', { 'is-txl is-z200': isLogin }]"
    >
      <el-form ref="loginForm" :model="loginForm" :rules="loginFormRules">
        <h2 class="title">欢迎使用携程旅游</h2>
        <span class="text"></span>
        <input class="form__input" type="text" placeholder="手机号" v-model="loginForm.phoneNumber"/>
        <br>

        <input class="form__input" type="password" placeholder="密码" v-model="loginForm.password"/>
        <div class="primary-btn" @click="submitLoginForm">立即登录</div>
      </el-form>
    </div>
    <div :class="['switch', { login: isLogin }]">
      <div class="switch__circle"></div>
      <div class="switch__circle switch__circle_top"></div>
      <div class="switch__container">
        <h2>{{ isLogin ? '你好，朋友!' : '欢迎回来 !' }}</h2>
        <p>
          {{
            isLogin
                ? '输入您的个人信息，与我们一起开始旅程'
                : '请输入您的个人信息登录，以保持与我们的联系'
          }}
        </p>
        <div class="primary-btn" @click="isLogin = !isLogin">
          {{ isLogin ? '立即注册' : '立即登录' }}
        </div>
      </div>
    </div>
  </div>
  </body>

</template>

<script>
import Cookies from "js-cookie";
import axios from "axios";

export default {
  name: 'LoginBox',
  data() {
    return {
      isLogin: false,

      //登录
      loginForm: {
        phoneNumber: "",
        password: ""
      },
      loginFormRules: {
        phoneNumber: [
          {required: true, message: "手机号必填", trigger: "blur"},
          {min: 11, max: 11, message: "手机号格式错误,请检查!", trigger: "blur"}
        ],
        password: [
          {required: true, message: "密码必填", trigger: "blur"},
          {min: 8, max: 20, message: "密码在8到20个字符之间", trigger: "blur"}
        ]
      },
      //注册
      registerForm: {
        phoneNumber: '',
        repass: ''
      },
      countdown: 0,
      timer: null,
      registerFormRules: {
        phoneNumber: [
          {required: true, message: "手机号必填", trigger: "blur"},
          {min: 11, max: 11, message: "手机号格式错误,请检查!", trigger: "blur"}
        ],
        repass: [
          {required: true, message: "验证码必填", trigger: "blur"},
          {min: 6, max: 6, message: "验证码格式错误,请检查!", trigger: "blur"}
        ]
      },
    }
  },
  methods: {

    sendCode() {
      // 发送验证码的逻辑
      if (!this.registerForm.phoneNumber) {
        this.$message.error('请输入手机号')
        return
      }
      // 发送验证码的异步请求
      // ...
      // 设置倒计时
      this.$axios.postForm("http://localhost:8084/Login/getCaptcha", this.registerForm)
          .then(reponse => {
            if (reponse.data.code == 200) {

              this.$message({
                message: "发送成功",
                type: "success",
                duration: 2000,
              })
            } else {
              this.$message({
                message: reponse.data.msg,
                type: "error",
                duration: 2000,
              })
            }
          })
      this.countdown = 60
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--
        } else {
          clearInterval(this.timer)
          this.timer = null
        }
      }, 1000)
    },
    submitForm() {
      this.$refs.registerForm.validate((validate) => {
        console.log(this.registerForm)

        if (!validate) {
          this.$message({
            message: "请正确填写表单",
            type: "error",
            duration: 2000,
          })
          return;
        }
        //请求后端接口进行校验
        // 第一个 url地址    第二个  请求参数
        this.$axios.post("http://localhost:8084/Login/register", this.registerForm)
            .then(reponse => {
              if (reponse.data.code === 200) {
                this.$message({
                  message: "注册成功,正在为您转跳主页",
                  type: "success",
                  duration: 2000,
                })
                this.$router.push("/");
                let responseData = reponse.data
                Cookies.set("token", responseData.result)
                // 将 token 存储到 localStorage
                localStorage.setItem('token', responseData.result);

                // 更新 axios 的默认请求头
                axios.defaults.headers.common['token'] = responseData.result;

                sessionStorage.setItem("token", responseData.data)
                document.cookie = "user = " + responseData.data;
              }
            })
      })
    },
    submitLoginForm() {

      this.$refs.loginForm.validate((validate) => {
        console.log(this.loginForm)

        if (!validate) {
          this.$message({
            message: "请正确填写表单",
            type: "error",
            duration: 2000,
          })

          return;
        }

        //请求后端接口进行校验
        // 第一个 url地址    第二个  请求参数
        console.log(this.loginForm.username)
        console.log(this.loginForm.password)
        this.$axios.post("http://localhost:8084/Login/login", this.loginForm)
            .then(reponse => {
              console.log(reponse)

              let responseData = reponse.data; //服务端响应的数据
              if (responseData.code == 200) {

                this.$message({
                  message: "登录成功",
                  type: "success",
                  duration: 2000,
                })
                console.log(responseData)
                //保存token到sessionStorage中,可以跨页面传递数据
                Cookies.set("token", responseData.result)
                // 将 token 存储到 localStorage
                localStorage.setItem('token', responseData.result);

                // 更新 axios 的默认请求头
                axios.defaults.headers.common['token'] = responseData.result;

                sessionStorage.setItem("token", responseData.data)
                document.cookie = "user = " + responseData.data;
                // localStorage.

                // this.$router.push("/"); 正常登陸完跳轉到首頁 這裡先測試 所以跳轉到測試頁 真正運行時用這個 把下面的刪了
                this.$router.push("/")
              } else {
                this.$message({
                  message: responseData.msg,
                  type: "error",
                  duration: 2000,
                })
              }
            })
      })
    }
  },
}
</script>

<style lang="scss" scoped>

html {
  background-color: #ecf0f3;
}

body {
  background-color: #ecf0f3;
}

.main-box {
  text-align: center;
  margin-left: 22%;
  position: relative;
  top: 10%;
  width: 1000px;
  min-width: 1000px;
  min-height: 600px;
  height: 600px;
  padding: 25px;
  background-color: #ecf0f3;
  box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
  border-radius: 12px;
  overflow: hidden;

  .send-code {
    position: absolute;
    top: 385px;
    right: 138px;
  }

  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    width: 600px;
    height: 100%;
    padding: 25px;
    background-color: #ecf0f3;
    transition: all 1.25s;

    form {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      color: #a0a5a8;

      .title {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }

      .text {
        margin-top: 30px;
        margin-bottom: 12px;
      }

      .form__input {
        width: 350px;
        height: 40px;
        margin: 4px 0;
        padding-left: 25px;
        font-size: 13px;
        letter-spacing: 0.15px;
        border: none;
        outline: none;
        // font-family: 'Montserrat', sans-serif;
        background-color: #ecf0f3;
        transition: 0.25s ease;
        border-radius: 8px;
        box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;

        &::placeholder {
          color: #a0a5a8;
        }
      }
    }
  }

  .container-register {
    z-index: 100;
    left: calc(100% - 600px);
  }

  .container-login {
    left: calc(100% - 600px);
    z-index: 0;
  }

  .is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
  }

  .is-z200 {
    z-index: 200;
    transition: 1.25s;
  }

  .switch {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 310px;
    padding: 50px;
    z-index: 200;
    transition: 1.25s;
    background-color: #ecf0f3;
    overflow: hidden;
    box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #f9f9f9;
    color: #a0a5a8;

    .switch__circle {
      position: absolute;
      width: 500px;
      height: 500px;
      border-radius: 50%;
      background-color: #ecf0f3;
      box-shadow: inset 8px 8px 12px #d1d9e6, inset -8px -8px 12px #f9f9f9;
      bottom: -60%;
      left: -60%;
      transition: 1.25s;
    }

    .switch__circle_top {
      top: -30%;
      left: 60%;
      width: 300px;
      height: 300px;
    }

    .switch__container {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: absolute;
      width: 400px;
      padding: 50px 55px;
      transition: 1.25s;

      h2 {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }

      p {
        font-size: 14px;
        letter-spacing: 0.25px;
        text-align: center;
        line-height: 1.6;
      }
    }
  }

  .login {
    left: calc(100% - 400px);

    .switch__circle {
      left: 0;
    }
  }

  .primary-btn {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    text-align: center;
    line-height: 50px;
    font-size: 14px;
    letter-spacing: 2px;
    background-color: #4b70e2;
    color: #f9f9f9;
    cursor: pointer;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;

    &:hover {
      box-shadow: 4px 4px 6px 0 rgb(255 255 255 / 50%),
      -4px -4px 6px 0 rgb(116 125 136 / 50%),
      inset -4px -4px 6px 0 rgb(255 255 255 / 20%),
      inset 4px 4px 6px 0 rgb(0 0 0 / 40%);
    }
  }
}
</style>